﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="KnowledgePointEdit.aspx.cs"
    Inherits="YiShi.KnowledgePoint.KnowledgePointEdit" %>

<%@ Register TagPrefix="Custom" TagName="KnowledgePointTree" Src="KnowledgePointTree.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link rel="stylesheet" href="/css/redmond/common.css" type="text/css" media="all" />
    <link rel="stylesheet" href="/css/redmond/jquery-ui-1.8.18.custom.css" type="text/css"
        media="all" />

    <script type="text/javascript" src="/scripts/jquery-1.7.1.min.js"></script>

    <script type="text/javascript" src="/scripts/jquery-ui-1.8.18.custom.min.js"></script>

    <script type="text/javascript" src="/scripts/common.js"></script>

</head>
<body style="font-size: 9pt;">
    <div align="center">
        <div align="right" style="width:960px">
            <button id="Insert">
                新增</button>
            <button id="Update">
                修改</button>
            <button id="Delete">
                删除</button>
            <button id="MoveUp">
                上移</button>
            <button id="MoveDown">
                下移</button>
        </div>
        <br>
        <Custom:KnowledgePointTree runat="server" ID="Search1" Width="960" />

        <script language="javascript">
            $(function event() {
                //Init
                $("button").button();
                $("#Update").click(function() {
                    var $ele = $("input:radio[name='Point']:checked");
                    if ($ele.length == 0) {
                        Alert("请选择要操作的节点！");
                        return;
                    }
                    FormEdit("PointEdit.aspx", "DataUpdate", { "Id": $ele.attr("value") }, 450, 130, function(jsondata) {
                        if (!jsondata)
                            return;
                        ShowMask();
                        jsondata["ActionType"] = "Update";
                        $.post("KnowledgePointEdit.aspx", jsondata, function(data) {
                            if (data.IsSuccess == "true") {
                                var $tr = $ele.parents("tr:first");
                                var $td = $tr.find("td:nth-child(2)");
                                var $namespan = $td.find("span[name='NameSpan']");
                                $namespan.text(jsondata["Name"]);
                                $namespan.css("color", jsondata["Type"] == "0" ? "Black" : "Gray");
                            }
                            else
                                ShowError(data.Message, data.Source, data.StackTrace);
                            HideMask();
                        }, "json");
                    });
                });
                $("#Insert").click(function() {
                    var $ele = $("input:radio[name='Point']:checked");
                    if ($ele.length == 0) {
                        Alert("请选择要操作的节点！");
                        return;
                    }
                    FormEdit("PointEdit.aspx", "DataInsert", null, 450, 130, function(jsondata) {
                        if (!jsondata)
                            return;
                        var $tr = $ele.parents("tr:first");
                        var level = parseInt($tr.attr("level"));
                        var $lastchild = $tr;
                        var $tmp = $tr.next();
                        while ($tmp.length != 0 && parseInt($tmp.attr("level")) > level) {
                            $lastchild = $tmp;
                            $tmp = $tmp.next();
                        }
                        var sortindex;
                        if (parseInt($lastchild.attr("level")) == level)
                            sortindex = 1000;
                        else
                            sortindex = parseInt($lastchild.attr("sortindex")) + 1000;
                        ShowMask();
                        jsondata["Id"] = $ele.attr("value");
                        jsondata["ActionType"] = "Insert";
                        jsondata["SortIndex"] = sortindex;
                        $.post("KnowledgePointEdit.aspx", jsondata, function(data) {
                            if (data.IsSuccess == "true") {
                                var $new = $("#TrTemplate").clone(true).removeAttr("id").attr("level", level + 1).attr("sortindex", sortindex);
                                var blank = "";
                                for (var i = 0; i < parseInt(level) + 1; i++)
                                    blank += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                                $new.find("input[name='Point']").val(data["Id"]);
                                var $td = $new.find("td:nth-child(2)");
                                $td.find("span[name='BlankSpan']").html(blank);
                                var $namespan = $td.find("span[name='NameSpan']");
                                $namespan.text(jsondata["Name"]);
                                $namespan.css("color", jsondata["Type"] == "0" ? "Black" : "Gray");
                                $new.css("display", "").insertAfter($lastchild);
                                if ($tr.find(".ui-icon-plusthick, .ui-icon-minusthick").length == 0) {
                                    $("#ExpandButtonTemplate").clone(true).removeAttr("id").insertAfter($tr.find("span[name='BlankSpan']"));
                                }
                            }
                            else
                                ShowError(data.Message, data.Source, data.StackTrace);
                            HideMask();
                        }, "json");
                    });
                });
                $("#Delete").click(function() {
                    if (!confirm("你确定要删除该结点么？"))
                        return;
                    var $ele = $("input:radio[name='Point']:checked");
                    if ($ele.length == 0) {
                        Alert("请选择要操作的节点！");
                        return;
                    }
                    var $tr = $ele.parents("tr:first");
                    var trlevel = parseInt($tr.attr("level"));
                    var $prev = $tr.prev();
                    if ($prev.attr("class") && $prev.attr("class") == "table-body") {
                        Alert("根节点不允许被删除！");
                        return;
                    }
                    var $next = $tr.next();
                    if ($next.length > 0 && parseInt($next.attr("level")) > trlevel) {
                        Alert("请先删除该节点的子节点！");
                        return;
                    }
                    ShowMask();
                    var jsondata = { "Id": $ele.attr("value"), "ActionType": "Delete" };
                    $.post("KnowledgePointEdit.aspx", jsondata, function(data) {
                        if (data.IsSuccess == "true") {
                            if ((parseInt($next.attr("level")) != trlevel) && (parseInt($prev.attr("level")) != trlevel)) {
                                $prev.find("button[name='ExpandButton']").remove();
                            }
                            $tr.remove();
                        }
                        else
                            ShowError(data.Message, data.Source, data.StackTrace);
                        HideMask();
                    }, "json");

                });
                $("#MoveUp").click(function() {
                    var $ele = $("input:radio[name='Point']:checked");
                    if ($ele.length == 0) {
                        Alert("请选择要操作的节点！");
                        return;
                    }
                    var $tr = $ele.parents("tr:first");
                    var level = $tr.attr("level");
                    var $trs = new Array();
                    $trs[$trs.length] = $tr;
                    var $next = $tr.next();
                    while ($next.length > 0 && parseInt($next.attr("level")) > level) {
                        $trs[$trs.length] = $next;
                        $next = $next.next();
                    }
                    var $prev = $tr.prev();
                    while ($prev.length > 0 && parseInt($prev.attr("level")) > level) {
                        $prev = $prev.prev();
                    }
                    if (parseInt($prev.attr("level")) == level) {
                        var sortindex;
                        var $prevprev = $prev.prev();
                        while (parseInt($prevprev.attr("level")) > level)
                            $prevprev = $prevprev.prev();
                        if (parseInt($prevprev.attr("level")) == level)
                            sortindex = parseInt((parseInt($prev.attr("sortindex")) + parseInt($prevprev.attr("sortindex"))) / 2);
                        else
                            sortindex = parseInt(parseInt($prev.attr("sortindex")) / 2);

                        ShowMask();
                        var jsondata = { "Id": $ele.attr("value"), "ActionType": "UpdateSortIndex", "SortIndex": sortindex };
                        $.post("KnowledgePointEdit.aspx", jsondata, function(data) {
                            if (data.IsSuccess == "true") {
                                for (var i = 0; i < $trs.length; i++) {
                                    $trs[i].insertBefore($prev);
                                }
                            }
                            else
                                ShowError(data.Message, data.Source, data.StackTrace);
                            HideMask();
                        }, "json");
                    }
                });
                $("#MoveDown").click(function() {
                    var $ele = $("input:radio[name='Point']:checked");
                    if ($ele.length == 0) {
                        Alert("请选择要操作的节点！");
                        return;
                    }
                    var $tr = $ele.parents("tr:first");
                    var level = $tr.attr("level");
                    var $trs = new Array();
                    $trs[$trs.length] = $tr;
                    var $next = $tr.next();
                    while ($next.length > 0 && parseInt($next.attr("level")) > level) {
                        $trs[$trs.length] = $next;
                        $next = $next.next();
                    }
                    var $tmp = $next.next();
                    while ($tmp.length > 0 && parseInt($tmp.attr("level")) > level) {
                        $next = $tmp;
                        $tmp = $tmp.next();
                    }
                    if (parseInt($next.attr("level")) >= level) {
                        var sortindex;
                        var $nextnext = $next.next();
                        while ($nextnext.length != 0 && parseInt($nextnext.attr("level")) > level)
                            $nextnext = $nextnext.next();
                        if ($nextnext.length != 0 && parseInt($nextnext.attr("level")) == level)
                            sortindex = parseInt((parseInt($next.attr("sortindex")) + parseInt($nextnext.attr("sortindex"))) / 2);
                        else
                            sortindex = parseInt($next.attr("sortindex")) + 1000;
                        ShowMask();
                        var jsondata = { "Id": $ele.attr("value"), "ActionType": "UpdateSortIndex", "SortIndex": sortindex };
                        $.post("KnowledgePointEdit.aspx", jsondata, function(data) {
                            if (data.IsSuccess == "true") {
                                for (var i = $trs.length - 1; i >= 0; i--) {
                                    $trs[i].insertAfter($next);
                                }
                            }
                            else
                                ShowError(data.Message, data.Source, data.StackTrace);
                            HideMask();
                        }, "json");
                    }
                });
            });
        </script>

    </div>
</body>
</html>
